/*******************************************************************************
 * Copyright 2011 Towee.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package net.towee.client.widgets.info;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Information popup
 * 
 * @author viiv.c58@gmail.com (Artem Artem'ev)
 */
public class InfoPopup {
	public static void showMessage(final Element el, Widget info) {
		int x = el.getAbsoluteLeft();
		int y = el.getAbsoluteTop();

		if (x + el.getOffsetWidth() > Window.getClientWidth())
			x -= el.getOffsetWidth();
		else
			x += el.getOffsetWidth();

		showMessage(x + 20, y, info);
	}

	public static void showMessage(final int x, final int y, Widget info) {
		// Init popup, if not init yet
		final PopupPanel popup = createPopUp(info);

		// Show popup
		popup.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
			public void setPosition(int offsetWidth, int offsetHeight) {
				popup.setPopupPosition(x, y);
			}
		});
	}

	private static PopupPanel createPopUp(Widget info) {
		// Create popup
		PopupPanel popup = new PopupPanel(true);
		popup.setStyleName("infoPopup");
		popup.getElement().getStyle().setZIndex(10000);

		// Add info widget
		popup.add(info);

		return popup;
	}
}
